/*
 * This file is part of the Jmesh.
 *
 * last modified: September 20, 10:00 AM
 *
 */
package br.ufmg.dees.insane.ui.rich.learn.util.input_output_dialogs;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 * This class provides a tabbed pane for the dialog.
 *
 * @author Pitangueira, Roque & Brugiolo, Marco 
 * @version 0.0.1 Beta  
 * @since jdk1.4.1
 */
public abstract class TabbedDialog extends JComponent implements ActionListener {


    public static final int OK_OPTION = 0;
    public static final int CANCEL_OPTION = 1;

    private int command;
    private JTabbedPane tabbedPane =new JTabbedPane();
    private JDialog dlg;
    private JPanel confirmationPane = new JPanel();
    private JButton ok = new JButton("OK");
    private JButton cancel = new JButton("CANCEL");

    public int showDialog(JFrame parent, String title) {
        dlg = new JDialog(parent, title, true);
        dlg.setSize(new Dimension(300, 200));
        dlg.setLocationRelativeTo(parent);
        dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dlg.setResizable(false);
        dlg.getContentPane().setLayout(new BorderLayout());
        dlg.getContentPane().add(tabbedPane, BorderLayout.CENTER);
        dlg.getContentPane().add(confirmationPane, BorderLayout.SOUTH);
        dlg.getRootPane().setDefaultButton(ok);

        //Sets up confirmation pane
        confirmationPane.add(ok);
        ok.addActionListener(this);

        cancel.setMnemonic(KeyEvent.VK_C);
        confirmationPane.add(cancel);
        cancel.addActionListener(this);
        dlg.pack();
        dlg.setVisible(true);
        return command;
    }

    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        if (btn.getActionCommand() == "OK"){
            command = OK_OPTION;
            approvedPerform();
        }else{
            command = CANCEL_OPTION;
        }
        dlg.setVisible(false);
    }

    abstract void approvedPerform();

    protected void addTab(String title, Component component) {
        tabbedPane.addTab(title, component);
    }
    
    protected void cancel() {
        command = CANCEL_OPTION;
    }
        
} // End of class

//==== End of File
